mobileData[[1]]

str(mobileData[[1]])


gps <- data.table(mobileData[[3]]@gps[, -2])
gps$Time <- as.POSIXct(gps$Time, format="%d/%m/%Y %H:%M:%S", tz="EST5EDT")
setkey(gps, Time)
argos <- data.table(mobileData[[3]]@argos)
setkey(argos, Loc..date)

head(gps)
head(argos)

argos$Loc..date <- as.POSIXct(argos$Loc..date, tz="GMT")
setkey(argos, Loc..date)

head(argos)
str(argos)

argos$Loc..date <- as.POSIXct(format(argos$Loc..date, tz="EST5EDT", usetz=TRUE))

n <- nrow(argos)
result <- data.frame(time=structure(numeric(n), class="POSIXct"), longitude=numeric(n), latitude=numeric(n), 
    arglong=numeric(n), arglat=numeric(n), LC=character(n), diffLong=numeric(n), diffLat=numeric(n), stringsAsFactors=FALSE, gpsinterval=numeric(n))
for (i in 1:n) {
  res <- gps[, Time > argos$Loc..date[i]]
  if (length(which(res)) > 0) {
    indexAfter <- which(res)[1]
    # Il existe une localisation GPS avant et apres
    if (indexAfter > 1) {
      # On recupere les localisations GPS juste avant et juste apres
      interval <- gps[c(indexAfter - 1, indexAfter), ]
      # On garde la difference entre les 2 localisations
      diff <- interval[2, ] - interval[1, ]
      
      # Calcul de la distance
      distance <- (diff$Longitude^2 + diff$Latitude^2)^0.5
      # Calcul de l'angle de deplacement
      angle <- atan(diff$Latitude/diff$Longitude)
      
      # Quand la localisation Argos survient apres la localisation GPS
      timeDiff <- difftime(argos$Loc..date[i], interval[1]$Time, units="secs")
      
      # Proportion par rapport a la duree entre les deux localisation GPS
      prop <- as.numeric(timeDiff) / as.numeric(diff$Time)
      # Distance theoriquement parcourue
      distanceDiff <- prop * distance
      
      if (distanceDiff == 0) {
        diffLat <- diffLong <- 0
      } else {
        # Difference de Latitude
        diffLat <- distanceDiff * sin(angle)
        # Difference de Longitude
        diffLong <- distanceDiff * cos(angle)
      }
      
      result$longitude[i] <- interval[1, Longitude] + diffLong
      result$latitude[i] <- interval[1, Latitude] + diffLat
      result$gpsinterval <- as.numeric(diff$Time)    
    } else {
      result$longitude[i] <- NA
      result$latitude[i] <- NA
      result$gpsinterval <- NA
    }
  } else {
    result$longitude[i] <- NA
    result$latitude[i] <- NA
    result$gpsinterval <- NA 
  }
  result$time[i] <- argos$Loc..date[i]
  result$arglong[i] <- argos$Longproj[i]
  result$arglat[i] <- argos$Latproj[i]
  result$LC[i] <- argos$Loc..quality[i]
  result$diffLat <- abs(result$arglat - result$latitude)
  result$diffLong <- abs(result$arglong - result$longitude)
}

result <- result[!is.na(result$longitude), ]

m <- aggregate(result[, c("diffLong", "diffLat")], by=list(result$LC),FUN=mean)
sd <- aggregate(result[, c("diffLong", "diffLat")], by=list(result$LC),FUN=sd)
n <- aggregate(result[, c("diffLat")], by=list(result$LC),FUN=length)




